package com.zjbti.canteenradio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_horn;
    private TextView tv_left_content,tv_right_content;
    private MyBroadcastReceiver receiver;
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        iv_horn=findViewById(R.id.iv_horn);
        tv_left_content=findViewById(R.id.tv_left_content);
        tv_right_content=findViewById(R.id.tv_right_content);

        iv_horn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_right_content.setText("开饭啦");
                Intent intent=new Intent();
                intent.setAction("open_rice");
                sendBroadcast(intent);
            }
        });
       receiver=new MyBroadcastReceiver();
       String action="open_rice";
        IntentFilter intentFilter=new IntentFilter();
        registerReceiver(receiver,intentFilter);



    }
    class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("open_rice")) {
                tv_left_content.setVisibility(View.VISIBLE);
                Log.d("MyBroadcastReceiver", "自定义的广播接收者，" + "接收到了发送开饭信号的广播信息");
            }
            Log.i("MyBroadcastReceiver",intent.getAction());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
